﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ladder : Nature
{
	Collider2D floorUp, step;

	private void Start() {
		floorUp = GetComponent<EdgeCollider2D>();
		step = GetComponent<BoxCollider2D>();
		offset = step.offset;
	}


	bool bo = false;
	Vector2 offset;
	private void Update() {

		// 左右轻微震动使得触发器一直处于触发状态
		int a = bo ? 1 : -1;
		step.offset = new Vector2(offset.x + Time.deltaTime * 0.5f, offset.y);
		bo = !bo;


		/* 太妙了 */
		// 根据Player上下键, 变化Collider2D位置, 避免上下阶梯处Bug(想好久, 还是这个好)
		float x = Input.GetAxis("Vertical");
		if (x > 0) {
			step.offset = new Vector2(offset.x, offset.y - 0.5f);
		} else if (x < 0) {
			step.offset = new Vector2(offset.x, offset.y + 0.5f);
		}

	}

	private void OnTriggerStay2D(Collider2D other) {
		// if (isFloor) return;    // 到达木板上了
		if (other.gameObject.tag == "Player") {
			if (!other.GetComponent<Player>().AllowClimb) return;
			bool isUp;
			other.GetComponent<Player>().OnClimbLadder(this, out isUp);
			if (isUp) {
				floorUp.GetComponent<PlatformEffector2D>().colliderMask = -1; // Everything
			} else {
				floorUp.GetComponent<PlatformEffector2D>().colliderMask = 0;  // Nothing
			}

		}
	}

	public void OnTriggerExit2D(Collider2D other) {
		if (other.gameObject.tag == "Player") {

			other.GetComponent<Rigidbody2D>().gravityScale = 1;
			other.GetComponent<Player>().SetPlayerState(PlayerState.Climb, false);  // Player关闭攀爬动画
		}
	}





	private void OnCollisionEnter2D(Collision2D other) {
		// floorUp.GetComponent<PlatformEffector2D>().colliderMask = -1; // Everything
	}

	private void OnCollisionStay2D(Collision2D other) {
		// float y = Input.GetAxis("Vertical");
		// Debug.Log(y);
		// if (y >= 0) return;
	}

	private void OnCollisionExit2D(Collision2D other) {
		// Debug.Log("Everything");
	}




}
